package com.junxonline.instant.admin.controller.monitor;

import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.monitor.OnlineUserVO;
import com.junxonline.instant.admin.service.monitor.OnlineUserService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 在线用户Controller
 *
 * @author JunX
 * @date 2021-07-06
 */
@Log4j2
@RestController
@RequestMapping(value = "/monitor/onlineUser")
public class OnlineUserController {

    @Autowired
    private OnlineUserService onlineUserService;

    /**
     * 查询在线用户列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "monitor:onlineUser:query")
    @OperationLog(label = "在线用户-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<OnlineUserVO> vo) {
        return CommonResult.success(onlineUserService.queryList(vo));
    }

    /**
     * 强制用户下线
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "monitor:onlineUser:delete")
    @OperationLog(label = "在线用户-强制用户下线", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        onlineUserService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
